// Copyright 2007 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package lichen.pages.admin.forum;

import java.util.List;

import lichen.entities.forum.Forum;
import lichen.entities.forum.ForumImpl;
import lichen.pages.Operations;
import lichen.pages.base.BasePage;
import lichen.services.ForumClassService;

import org.apache.tapestry.annotations.Component;
import org.apache.tapestry.annotations.Inject;
import org.apache.tapestry.corelib.components.Loop;

/**
 * 展示论坛类别的页面
 * @author xiafei
 * @author <a href="mailto:jun.tsai@gmail.com">Jun Tsai</a>
 * @version $Revision: 147 $
 * @since 0.0.1
 */
public class ViewForum extends BasePage {
	//通过左边距对类别进行排序
	private static final String QUERY_ALL_FORUM_HQL = "from "
			+ ForumImpl.class.getName() + " f order by f."+ForumImpl.LEFT_PRO_NAME;

	@Inject
	private ForumClassService forumClassService;

	@SuppressWarnings("unused")
	@Component(parameters = { "source=forums", "value=forum" })
	private Loop forumsLoop;

	//循环使用的变量forum
	private Forum forum;

	/**
	 * 得到编辑论坛时候的参数.
	 * @return 编辑论坛的参数.
	 */
	public Object[] getEditParameters() {
		return new Object[] { Operations.EDIT, forum};
	}
	/**
	 * 得到增加子项的参数.
	 * @return 增加子项的参数列表.
	 */
	public Object[] getAddChildParameters() {
		return new Object[] { Operations.ADD_CHILD, forum};
	}
	
	/**
	 * 查询所有的论坛.
	 * 
	 * @return 论坛列表.
	 */
	public List getForums() {
		return this.getEntityService().find(QUERY_ALL_FORUM_HQL);
	}
	/**
	 * 设定论坛类别实体.
	 * @param forum 论坛类别实体.
	 */
	public void setForum(Forum forum) {
		this.forum = forum;
	}
	/**
	 * 得到论坛类别实体.
	 * @return 论坛类别实体
	 */
	public Forum getForum() {
		return this.forum;
	}

	
	/**
	 * 删除一个论坛类别.
	 * @param forumId 论坛的ID.
	 */
	void onActionFromDeleteLink(Forum forum) {
		forumClassService.deleteTreeNode(forum, null);
	}
	/**
	 * 删除一个论坛类别.
	 * @param forumId 论坛的ID.
	 */
	void onActionFromMoveUpLink(Forum forum) {
		forumClassService.moveNode(forum,1, null);
	}
	/**
	 * 删除一个论坛类别.
	 * @param forumId 论坛的ID.
	 */
	void onActionFromMoveDownLink(Forum forum) {
		forumClassService.moveNode(forum,-1, null);
	}

	
	/**
	 * 得到展示论坛的css类名.
	 */
	public String getForumCssClassName() {
		if (this.getForum().getDepth() == 1) {//大类。
			return "class-header";
		} else {
			return "forum-header";
		}
	}
}
